package com.yanqu.road.server.cmd.activity.springunionbanquet;

import com.yanqu.road.entity.log.springunionbanquet.LogSpringUnionBanquetOpen;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SpringUnionBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.springunionbanquet.SpringUnionBanquetMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = Protocol.S_SPRING_UNION_BANQUET_OPEN_FROM_CROSS, desc = "宴请（返回）")
public class SpringUnionBanquetOpenFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        SpringUnionBanquetProto.CrossSpringUnionBanquetOpenRespMsg respMsg = SpringUnionBanquetProto.CrossSpringUnionBanquetOpenRespMsg.parseFrom(packet.getBytes());
        int goodsId = respMsg.getGoodsId();
        Property consume = PropertyHelper.parseStringToProperty(goodsId + "=1");

        if(respMsg.getRet() == 0){

            //日志
            AutoLogMgr.add(new LogSpringUnionBanquetOpen(
                    SpringUnionBanquetMgr.getConfig().getActivityId(),
                    packet.getPlayerId(),
                    respMsg.getBanquet().getBanquetId(),
                    respMsg.getBanquet().getConfigId()));

           player.getModule(PreReduceModule.class).restorePreReduce(consume);

           player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.SpringUnionBanquet, eLogMoneyType.SpringUnionBanquetOpenConsume);
        }

        SpringUnionBanquetProto.SpringUnionBanquetOpenRespMsg.Builder builder = SpringUnionBanquetProto.SpringUnionBanquetOpenRespMsg.newBuilder();
        builder.setRet(respMsg.getRet());
        if(respMsg.hasBanquet()) {
            builder.setBanquet(respMsg.getBanquet());
        }
        player.sendPacket(Protocol.U_SPRING_UNION_BANQUET_OPEN, builder);

    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
